/*
    编辑商品:所有商品
 */
$(function () {
    $("#jqGrid").Grid({
        height: "700px",
        url: '../product/listByParam',
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '编号', name: 'id', index: 'id', width: 30},
            {label: '产品名称', name: 'name', index: 'name', width: 70},
            {label: '价格', name: 'price', index: 'price', width: 30},
            {label: '旧价格', name: 'oldPrice', index: 'old_price', width: 30},
            {label: 'SPC价格', name: 'spcPrice', index: 'spcPrice', width: 30},
            {label: '数量', name: 'number', index: 'number', width: 30},
            {
                label: '状态', name: 'statu', index: 'statu', width: 30, formatter: function (value) {
                    if (value == '0') {
                        return '<span class="label label-warning" style="padding: 5px">下&nbsp;架</span>';
                    } else if (value == '1') {
                        return '<span class="label label-primary" style="padding: 5px">上&nbsp;架</span>';
                    }else{
                        return '';
                    }
                }
            },

            {
                label: '产品类型', name: 'type', index: 'type', width: 40,
                formatter: function (value) {
                    if (value == 1) {
                        return '<span class="label-success" style="padding: 5px">价格产品</span>';
                    }
                    if (value == 2) {
                        return '<span class="label-warning" style="padding: 5px">兑换产品</span>';
                    }
                    if (value == 3) {
                        return '<span class="label-info" style="padding: 5px">余额兑换产品</span>';
                    }
                    if (value == 4) {
                        return '<span class="label-info" style="padding: 5px">D-one产品</span>';
                    }
                    if (value == 5) {
                        return '<span class="label-info" style="padding: 5px">汽车产品</span>';
                    }
                    return '<span class="label-error">其他</span>';
                }
            },

            {label: '描述', name: 'describ', index: 'describ', width: 80},
            {
                label: '头图', name: 'imageList', index: 'imageList', width: 40, formatter: function (value) {
                    if (value != null) {
                        return transImg(value);
                    }
                    return '';

                }
            },
            {label: '商家名', name: 'merchantName', index: 'merchantName', width: 40},
            {label: '产品页面路径', name: 'productUrl', index: 'product_url', width: 80},
            {label: '一级分类', name: 'cateL1Name', index: 'cateL1Name', width: 40},
            {label: '二级分类', name: 'cateL2Name', index: 'cateL2Name', width: 40},
            {label:'审核',name:'auditStatus',index:'auditStatus',width:40,
                formatter:function (cellvalue, options, row) {
                    if(row.auditStatus==0){
                        return "";
                    } else if(row.auditStatus==1){
                        return '<a href="#" onclick="vm.auditSuccess(\''+row.id+'\')">通过</a>   '+'<a href="#" onclick="vm.auditFail(\''+row.id+'\')">拒绝</a>';
                    }else if(row.auditStatus==2){
                        //return "通过";
                        return '<span class="label label-primary" style="padding: 5px">通&nbsp;过</span>';
                    }else if(row.auditStatus==3){
                        //return "拒绝";
                        return '<span class="label label-warning" style="padding: 5px">拒&nbsp;绝</span>';
                    }
                }
            },
            {label: '说明路径', name: 'statementUrl', index: 'statement_url', width: 80}
        ]
    });
    $('#productbody').editable({
        inlineMode: false,
        alwaysBlank: true,
        height: '500px', //高度
        minHeight: '200px',
        language: "zh_cn",
        spellcheck: false,
        plainPaste: true,
        enableScript: false,
        imageButtons: ["floatImageLeft", "floatImageNone", "floatImageRight", "linkImage", "replaceImage", "removeImage"],
        allowedImageTypes: ["jpeg", "jpg", "png", "gif"],
        imageUploadURL: '../sys/oss/upload',
        imageUploadParams: {prefix: "productImage"},
        imagesLoadURL: '../sys/oss/queryAll'
    });
    Ajax.request({
        url: "../sys/merchant/queryMerchant",
        async: true,
        successCallback: function (r) {
            vm.merchants = [];
            vm.merchants = r.list;
        }
    });

});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        cateSelect: false,
        categorysL1:[],
        categorysL2:[],
        styles:[],
        payList:[1,2],
        proStyle: {},
        addProStyle:{},
        title: null,
        product: {
            cateL1 : ''
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        dimensions: [],
        q: {
            name: '',
            type: '',
            auditStatus:'',
            merchantId:'',
            status:''
        },
        merchants:[]
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.product = {};
            vm.styles = [];
            $('#productbody').editable('setHTML', '');
            vm.getCategorys();
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let url = vm.product.id == null ? "../product/saveAudit" : "../product/updateAudit";
            //vm.product.body = $('#probody').editable('getHTML');
            vm.product.body = $('#productbody').editable('getHTML');

            vm.product.select = vm.payList;
            if (vm.styles.length == "0"){
                iview.Message.error("请为产品添加一样款式");
                return;
            }
            if (vm.product.cateL1 == null || vm.product.cateL1 === ''){
                iview.Message.error("请选择一级分类");
                return;
            }
            if (vm.product.cateL2 == null || vm.product.cateL2 === ''){
                iview.Message.error("请选择二级分类");
                return;
            }
            vm.product.proStyles = vm.styles;
            Vue.delete(this.product,'way');
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.product),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });

        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../productnew/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../product/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.product = r.product;
                    vm.payList = r.product.way;
                    $('#productbody').editable('setHTML', vm.product.body);
                    vm.styles = r.product.proStyles;
                }
            });
        },
        getCategorys: function () {
            vm.categorysL2 = [];
            Ajax.request({
                url: "../category/queryAll?cateL1="+vm.product.cateL1,
                async: true,
                successCallback: function (r) {
                    vm.categorysL1 = r.listL1;
                    vm.categorysL2 = r.listL2;
                }
            });
        },
        delstyle: function (index) {
            this.styles.splice(index,1);
        },
        addstyle:function(){
            if (vm.addProStyle.name == null || vm.addProStyle.name === ''){
                iview.Message.error("请填写款式名称");
                return;
            }
            if (vm.addProStyle.price == null || vm.addProStyle.price === ''){
                iview.Message.error("请填写款式价格");
                return;
            }
            if (vm.addProStyle.oldPrice == null || vm.addProStyle.oldPrice === ''){
                iview.Message.error("请填写款式活动价格");
                return;
            }
            if (vm.addProStyle.imageUrl == null || vm.addProStyle.imageUrl === ''){
                iview.Message.error("请填写款式图片地址");
                return;
            }
            let Pstyle = {
                "name": vm.addProStyle.name,
                "price":vm.addProStyle.price,
                "oldPrice":vm.addProStyle.oldPrice,
                "imageUrl":vm.addProStyle.imageUrl,
                "remark":vm.addProStyle.remark
            }
            vm.styles.push(Pstyle);
            vm.addProStyle = {};
        },
        reload: function (event) {
            vm.styles = [];
            vm.categorysL1=[],
                vm.categorysL2=[],
                vm.payList=[1,2];
            vm.product= {
                cateL1 : ''
            };
            vm.dimensions = [];
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name, "type": vm.q.type,"auditStatus":vm.q.auditStatus,'merchantId':vm.q.merchantId,'status':vm.q.status},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: '',
                type: '',
                auditStatus:'',
                merchantId:'',
                status:''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleSuccessPicUrl: function (res, file) {
            vm.product.imageList = file.response.url;
        },
        handleSuccessHomePic: function (res, file) {
            vm.product.other2 = file.response.url;
        },
        handleSuccessProImage: function (res, file) {
            vm.addProStyle.imageUrl = file.response.url;
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        eyeImagePicUrl: function () {
            var url = vm.product.imageList;
            eyeImage(url);
        },
        eyeImageHomePic: function () {
            var url = vm.product.other2;
            eyeImage(url);
        },
        eyeImageProImage: function () {
            var url = vm.addProStyle.imageUrl;
            eyeImage(url);
        },
        eyeImage: function (e) {
            eyeImage($(e.target).attr('src'));
        },auditSuccess:function (id) {
            Ajax.request({
                url: "../product/auditSuccess?id=" + id+"&status=2",
                async: true,
                successCallback: function (r) {
                    vm.reload();
                }
            });
        },auditFail:function (id) {
            Ajax.request({
                url: "../product/auditSuccess?id=" + id+"&status=3",
                async: true,
                successCallback: function (r) {
                    vm.reload();
                }
            });
        },auditAll:function () {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null){
                return;
            }
            Ajax.request({
                url: "../product/auditAll",
                params: JSON.stringify(ids),
                type: "POST",
                contentType: "application/json",
                successCallback: function () {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        }
    }
});